package business.impl;

import java.util.List;

import model.TAtomicauthorityB;
import model.TRoleB;
import model.TUser;
import model.VAtomicauthority;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.AtomDao;
import business.dao.RoleDao;
import business.dao.UserDao;
import business.factory.DAOFactory;

public class AtomDaoImpl implements AtomDao {
	
	iHibBaseDAO bdao = null;
	public void setBdao (iHibBaseDAO bdao){
		this.bdao=bdao;
		
	}

	@Override
	public TAtomicauthorityB getAtomById(int id) {
		TAtomicauthorityB title = (TAtomicauthorityB) bdao.findById(TAtomicauthorityB.class,id);
		return title;
	}

	@Override
	public int addAtom(TAtomicauthorityB model) {
		Object obj = bdao.insert(model);
		if (obj == null) {
			return 0;
		}
		return 1;
	}

	@Override
	public boolean upAtom(TAtomicauthorityB model) {
		return bdao.update(model);
	}

	@Override
	public boolean delAtom(int id) {
		return bdao.delete(TAtomicauthorityB.class, id);
	}

	@Override
	public List<VAtomicauthority> getAtoms() {
		String sql = " from VAtomicauthority ";
		List<VAtomicauthority> modl = bdao.select(sql);
		return modl;
	}

	@Override
	public int getAtomsbyuserid(int userid, int num) {
		
		UserDao rDao = DAOFactory.getUserDAO();
		TUser model = rDao.getUserById(userid);
		
		
		String sql = " from TAtomicauthorityB where roleID = ?  authorityID = ? ";
		Object[] para = {model.getUsertype(),num};
		int modl = bdao.selectValue(sql,para);
		return modl;
	}

	@Override
	public List<VAtomicauthority> getAtomsby(int roleid, int num, String autname) {
		String sql = " from VAtomicauthority where isDel = 0 ";
		
		if(roleid != 0){
			sql += " and roleID = ? ";
		}
		if(roleid != 0){
			sql += " and authoritynum = ? ";
		}
		if(autname != ""){
			sql += " and authorityName = ? ";
		}
		
		List<VAtomicauthority> list = bdao.select(sql);
		return list;
	}

	
}
